package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Courses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.CourseregistrationsMapper;
import com.ruoyi.system.domain.Courseregistrations;
import com.ruoyi.system.service.ICourseregistrationsService;

/**
 * 课程报名信息Service业务层处理
 * 
 * @author 张英旺
 * @date 2024-02-27
 */
@Service
public class CourseregistrationsServiceImpl implements ICourseregistrationsService 
{
    @Autowired
    private CourseregistrationsMapper courseregistrationsMapper;

    /**
     * 查询课程报名信息
     * 
     * @param registrationId 课程报名信息主键
     * @return 课程报名信息
     */
    @Override
    public Courseregistrations selectCourseregistrationsByRegistrationId(Long registrationId)
    {
        return courseregistrationsMapper.selectCourseregistrationsByRegistrationId(registrationId);
    }

    /**
     * 查询课程报名信息列表
     * 
     * @param courseregistrations 课程报名信息
     * @return 课程报名信息
     */
    @Override
    public List<Courseregistrations> selectCourseregistrationsList(Courseregistrations courseregistrations)
    {
        return courseregistrationsMapper.selectCourseregistrationsList(courseregistrations);
    }

    /**
     * 新增课程报名信息
     * 
     * @param courseregistrations 课程报名信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCourseregistrations(Courseregistrations courseregistrations)
    {
        courseregistrations.setCreateTime(DateUtils.getNowDate());
        int rows = courseregistrationsMapper.insertCourseregistrations(courseregistrations);
        insertSysUser(courseregistrations);
        return rows;
    }

    /**
     * 修改课程报名信息
     * 
     * @param courseregistrations 课程报名信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCourseregistrations(Courseregistrations courseregistrations)
    {
        courseregistrations.setUpdateTime(DateUtils.getNowDate());
        courseregistrationsMapper.deleteSysUserByUserId(courseregistrations.getRegistrationId());
        insertSysUser(courseregistrations);
        return courseregistrationsMapper.updateCourseregistrations(courseregistrations);
    }

    /**
     * 批量删除课程报名信息
     * 
     * @param registrationIds 需要删除的课程报名信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCourseregistrationsByRegistrationIds(Long[] registrationIds)
    {
        courseregistrationsMapper.deleteSysUserByUserIds(registrationIds);
        return courseregistrationsMapper.deleteCourseregistrationsByRegistrationIds(registrationIds);
    }

    /**
     * 删除课程报名信息信息
     * 
     * @param registrationId 课程报名信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCourseregistrationsByRegistrationId(Long registrationId)
    {
        courseregistrationsMapper.deleteSysUserByUserId(registrationId);
        return courseregistrationsMapper.deleteCourseregistrationsByRegistrationId(registrationId);
    }

    /**
     * 新增用户信息信息
     * 
     * @param courseregistrations 课程报名信息对象
     */
    public void insertSysUser(Courseregistrations courseregistrations)
    {
        List<SysUser> sysUserList = courseregistrations.getSysUserList();
        Long registrationId = courseregistrations.getRegistrationId();
        if (StringUtils.isNotNull(sysUserList))
        {
            List<SysUser> list = new ArrayList<SysUser>();
            for (SysUser sysUser : sysUserList)
            {
                sysUser.setUserId(registrationId);
                list.add(sysUser);
            }
            if (list.size() > 0)
            {
                courseregistrationsMapper.batchSysUser(list);
            }
        }
    }
    /**
     * 新增健身课程信息信息
     *
     * @param courseregistrations 课程报名信息对象
     */
    public void insertCourses(Courseregistrations courseregistrations)
    {
        List<Courses> coursesList = courseregistrations.getCoursesList();
        Long registrationId = courseregistrations.getRegistrationId();
        if (StringUtils.isNotNull(coursesList))
        {
            List<Courses> list = new ArrayList<Courses>();
            for (Courses courses : coursesList)
            {
                courses.setCourseId(registrationId);
                list.add(courses);
            }
            if (list.size() > 0)
            {
                courseregistrationsMapper.batchCourses(list);
            }
        }
    }
}
